package com.ljz.servlet;

import com.google.gson.Gson;
import com.ljz.service.SearchKeyService;
import com.ljz.service.impl.SearchKeyServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


@WebServlet("/searchKey")
public class SearchKeyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        req.setCharacterEncoding("utf-8");
//        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();
        String key = req.getParameter("key");
        SearchKeyService searchKeyService = new SearchKeyServiceImpl();
        List<String> list = searchKeyService.searchKey(key);
        Gson gson = new Gson();
        String json = gson.toJson(list);
        out.print(json);
    }
}
